package com.ebuy.it.execludedemo.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <p>
 *  持久化配置
 * </p>
 *
 * @author lizhe
 * @since 2019-3-7
 */
@Configuration
@MapperScan({"com.ebuy.it.execludedemo.mapper.*"})
public class MybatisPlusConfig {

    /**
     * mapper-plus分页插件<br>
     * 文档：http://mp.baomidou.com<br>
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        //paginationInterceptor.setLocalPage(true);// 开启 PageHelper 的支持
        return paginationInterceptor;
    }

    /**
     * mapper-plus SQL执行效率插件【生产环境可以关闭】
     */
//    @Bean
//    public PerformanceInterceptor performanceInterceptor() {
//        return new PerformanceInterceptor();
//    }
//
//    @Bean(name = "mrpprod")
//    @ConfigurationProperties(prefix = "spring.datasource.druid.mrpprod" )
//    public DataSource mrpprod () {
//        return DruidDataSourceBuilder.create().build();
//    }

//    @Bean(name = "mrpjava")
//    @ConfigurationProperties(prefix = "spring.datasource.druid.mrpjava" )
//    public DataSource mrpjava () {
//        return DruidDataSourceBuilder.create().build();
//    }

//    @Bean(name = "mrpbi")
//    @ConfigurationProperties(prefix = "spring.datasource.druid.mrpbi" )
//    public DataSource mrpbi () {
//        return DruidDataSourceBuilder.create().build();
//    }
	
//    @Bean(name = "overseasmrp")
//    @ConfigurationProperties(prefix = "spring.datasource.druid.overseasmrp" )
//    public DataSource overseasmrp () {
//        return DruidDataSourceBuilder.create().build();
//    }

//    @Bean(name = "plancommon")
//    @ConfigurationProperties(prefix = "spring.datasource.druid.plancommon" )
//    public DataSource plancommon () {
//        return DruidDataSourceBuilder.create().build();
//    }
//
//    @Bean(name = "planstock")
//    @ConfigurationProperties(prefix = "spring.datasource.druid.planstock" )
//    public DataSource planstock () {
//        return DruidDataSourceBuilder.create().build();
//    }
//    @Bean(name = "datacenter")
//    @ConfigurationProperties(prefix = "spring.datasource.druid.datacenter" )
//    public DataSource datacenter () {
//        return DruidDataSourceBuilder.create().build();
//    }
//    @Bean(name = "purchase")
//    @ConfigurationProperties(prefix = "spring.datasource.druid.purchase" )
//    public DataSource purchase () {
//        return DruidDataSourceBuilder.create().build();
//    }
//    @Bean(name = "wms")
//    @ConfigurationProperties(prefix = "spring.datasource.druid.wms" )
//    public DataSource wms () {
//        return DruidDataSourceBuilder.create().build();
//    }
//    @Bean(name = "wms2nb")
//    @ConfigurationProperties(prefix = "spring.datasource.druid.wms2nb" )
//    public DataSource wms2nb () {
//        return DruidDataSourceBuilder.create().build();
//    }
//    @Bean(name = "wms2dg")
//    @ConfigurationProperties(prefix = "spring.datasource.druid.wms2dg" )
//    public DataSource wms2dg () {
//        return DruidDataSourceBuilder.create().build();
//    }
//    /**
//     * 动态数据源配置
//     * @return
//     */
//    @Bean
//    @Primary
//    public DataSource multipleDataSource (@Qualifier("mrpprod") DataSource mrpprod,
////                                          @Qualifier("mrpjava") DataSource mrpjava,
////                                          @Qualifier("overseasmrp") DataSource overseasmrp,
//                                          @Qualifier("plancommon") DataSource plancommon,
////                                          @Qualifier("mrpbi") DataSource mrpbi,
//                                          @Qualifier("planstock") DataSource planstock,
//                                          @Qualifier("datacenter") DataSource datacenter,
//                                          @Qualifier("purchase") DataSource purchase,
//                                          @Qualifier("wms") DataSource wms,
//                                          @Qualifier("wms2nb") DataSource wms2nb,
//                                          @Qualifier("wms2dg") DataSource wms2dg
//
//    ) {
//        DynamicDataSource dynamicDataSource = new DynamicDataSource();
//        Map< Object, Object > targetDataSources = new HashMap<>();
//        targetDataSources.put(DBTypeEnum.mrpprod.getValue(), mrpprod );
////        targetDataSources.put(DBTypeEnum.mrpjava.getValue(), mrpjava);
//        targetDataSources.put(DBTypeEnum.plancommon.getValue(), plancommon);
////		targetDataSources.put(DBTypeEnum.overseasmrp.getValue(), overseasmrp);
////        targetDataSources.put(DBTypeEnum.mrpbi.getValue(), mrpbi);
//
//        targetDataSources.put(DBTypeEnum.planstock.getValue(), planstock);
//        targetDataSources.put(DBTypeEnum.datacenter.getValue(), datacenter);
//        targetDataSources.put(DBTypeEnum.purchase.getValue(), purchase);
//        targetDataSources.put(DBTypeEnum.wms.getValue(), wms);
//        targetDataSources.put(DBTypeEnum.wms2nb.getValue(), wms2nb);
//        targetDataSources.put(DBTypeEnum.wms2dg.getValue(), wms2dg);
//
//        dynamicDataSource.setTargetDataSources(targetDataSources);
//        dynamicDataSource.setDefaultTargetDataSource(mrpprod);
//        return dynamicDataSource;
//    }

//    @Bean("sqlSessionFactory")
//    public SqlSessionFactory sqlSessionFactory() throws Exception {
//        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
//        sqlSessionFactory.setDataSource(multipleDataSource(mrpprod(),
////                mrpjava(),
////                mrpbi(),
////                overseasmrp(),
//                plancommon(),
//                planstock(),
//                datacenter(),
//                purchase(),
//                wms(),
//                wms2nb(),
//                wms2dg()
//        ));
//        sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/*/*Mapper.xml"));
//
//        MybatisConfiguration configuration = new MybatisConfiguration();
//        //configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
//        configuration.setJdbcTypeForNull(JdbcType.NULL);
//        configuration.setMapUnderscoreToCamelCase(true);
//        configuration.setCacheEnabled(false);
//        sqlSessionFactory.setConfiguration(configuration);
//        sqlSessionFactory.setPlugins(new Interceptor[]{ //PerformanceInterceptor(),OptimisticLockerInterceptor()
//                paginationInterceptor()
//        });
//        //  sqlSessionFactory.setGlobalConfig(globalConfiguration());
//        return sqlSessionFactory.getObject();
//    }
}
